package com.ld.controller;

import com.ld.entity.*;
import com.ld.service.StoreService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/storeController")
public class StoreController {

    @Resource(name = "storeService")
    private StoreService storeServiceImpl;

    /**
     * @param filmId 传入电影id
     * @return 有则返回店铺信息list<Store>,包括这部电影在这家店的存货 无则返回null
     */
    @PostMapping("/getCurrentFilmCount")
    @ResponseBody
    public List<Store> getCurrentFilmCount(@RequestParam("filmId") Integer filmId) {
        List<Store> storeInfo = storeServiceImpl.getStoreCurrentFilmCount(filmId);
        if (storeInfo != null && storeInfo.size() > 0){
            return storeInfo;
        }
        return null;
    }

    /**这家店所有尚未归还的信息
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @return 这家店所有尚未归还的信息
     * allUnreturnedInfo获取未归还的list<Rental>
     * pageInfo获取pageinfo
     */
    @PostMapping("/getAllUnreturnedInfo")
    @ResponseBody
    public Map<String, Object> getAllUnreturnedInfo(
                                 @RequestParam("currentPage") int currentPage,
                                 @RequestParam("pageSize")int pageSize){
        Session session = SecurityUtils.getSubject().getSession();
        Staff loginUser = (Staff)session.getAttribute("loginUser");
        //从session中获取当前登录的员工隶属于哪家店
        Integer storeId = loginUser.getStoreId();
        return storeServiceImpl.getAllUnreturnedInfo(storeId, currentPage, pageSize);
    }


    /**
     * 结算
     * @param rental 有值的部分:rentalId,inventoryId,filmId,customerId,returnDate,amount
     *            (staffId和storeId需要从session中获取)
     * @return 成功与否消息
     */
    @PostMapping("/clearing")
    @ResponseBody
    public String clearing(Rental rental){
        Session session = SecurityUtils.getSubject().getSession();
        Staff loginUser = (Staff)session.getAttribute("loginUser");
        //设置结算操作的员工id
        rental.setReturnStaffId(loginUser.getStaffId());
        //设置结算操作的员工属于哪个店铺
        rental.setStoreId(loginUser.getStoreId());
        return storeServiceImpl.clearing(rental);
    }


    /**
     *
     * @param searchType 查询类型 按电影名,按电影编号,按电影类型,按电影等级,按演员名称
     * @param searchContent 搜索内容
     * @return 返回查询结果
     */
    @PostMapping("/searchByType")
    @ResponseBody
    public Object searchByType(@RequestParam("searchType") String searchType,
                             @RequestParam("searchContent") String searchContent){

        Session session = SecurityUtils.getSubject().getSession();
        Staff loginUser = (Staff)session.getAttribute("loginUser");
        //从session中获取当前登录的员工隶属于哪家店 (只能查询自家店的东西)
        Integer storeId = loginUser.getStoreId();

        Map<String, Object> resultMap = storeServiceImpl.searchByType(storeId, searchType, searchContent);
        if (resultMap!=null){
            //无论查询什么都会是list,只是不知道list的类型而已

            Object  resultList =resultMap.get(searchType);
            return resultList;
        }
        return "error";
    }

    /**
     * 根据演员id查询这个演员所参与的电影,只能查询自己店铺的电影
     * @param actorId 演员id
     * @return film中的film_id,title,rating,category,inventory_id,isRental
     */
    @PostMapping("/getWhichFilmTheActorWereInvolvedIn")
    @ResponseBody
    public List<Film> getWhichFilmTheActorWereInvolvedIn(@RequestParam("actorId") Integer actorId){
        Session session = SecurityUtils.getSubject().getSession();
        Staff loginUser = (Staff)session.getAttribute("loginUser");
        // 从session中获取 storeId 店铺id
        Integer storeId = loginUser.getStoreId();
        return   storeServiceImpl.getWhichFilmTheActorWereInvolvedIn(actorId,storeId);
    }


    /**
     * 借出
     * @param customerId 借出的顾客id
     * @param rentalTime 借出的时间
     * @param inventoryId 库存id
     * @return 信息
     */
    @PostMapping("/getCustomerRental")
    @ResponseBody
    public String getCustomerRental(@RequestParam("customerId") Integer customerId,
                                    @RequestParam("rentalTime") String rentalTime,
                                    @RequestParam("inventoryId")Integer inventoryId){

        Session session = SecurityUtils.getSubject().getSession();
        Staff loginUser = (Staff)session.getAttribute("loginUser");
        //从session中获取员工id
        Integer staffId = loginUser.getStaffId();
        //此员工隶属于哪家店
        Integer storeId = loginUser.getStoreId();
        Rental rental = new Rental();
        rental.setCustomerId(customerId);
        rental.setRentalDate(rentalTime);
        rental.setInventoryId(inventoryId);
        rental.setStaffId(staffId);//哪个员工经手借出的
        rental.setStoreId(storeId);
        String msg = storeServiceImpl.getCustomerRental(rental);
        System.out.println(rental);
        return msg;
    }

}
